<?php
/**
 * Created by PhpStorm.
 * User: xuzhong
 * Date: 2025/4/2
 * Time: 13:49
 */

namespace app\api\service;

use app\api\model\Article as ArticleModel;
use app\api\model\Images;
use app\facade\Basic;
use phpseclib3\File\ASN1\Maps\IssuerAltName;

class Article
{

    protected $model;

    public function __construct()
    {
        $this->model = new ArticleModel();

    }

    /**
     * @describe 获取销售人员列表
     * @author: 徐众
     * @date: 2025-04-02 13:02:26
     * @param [] type
     * @return Renderable
     */
    public function getList($params,$page, $limit): array
    {
        #验证参数
        if (0 >= $page || 100 < $limit) {
            return Basic::data( 100002, 'common.parameterError');
        }
        #处理查询条件
        $where = ['is_del' => 0];

        if(isset($params['type']) && !empty($params['type'])){
            $where['type'] = $params['type'];
        }
        $sql = $this->model->where($where);
        if(isset($params['category']) && !empty($params['category'])){
            $sql = $sql->whereIn('category',$params['category']);
        }
        if(isset($params['keywords']) && !empty($params['keywords'])){
            $sql = $sql->whereRaw(
                "title like ? or content like ? " ,['%'.$params['keywords']."%" ,'%'.$params['keywords']."%" ]
            );
        }
        #取数据
        $total = $sql->count();
        if (!$total) {
            return Basic::data(0, 'common.success', ['list' => [], 'total' => 0]);
        }

        $list = $sql->page($page, $limit)->order('id DESC')->select()->toArray();
        $photoId = array_unique(array_column($list,'photo'));
        #获取图片url
        $oImages = new Images();
        $photoUrls = $oImages->getImagesByIds($photoId);
        $avatorUrls = array_column($photoUrls, null, 'id');
        #替换url
        foreach ($list as $k=>$v){
            $list[$k]['photo'] = $avatorUrls[$v['photo']]['url']??'';
            $list[$k]['name'] = $avatorUrls[$v['photo']]['name']??'';
            $list[$k]['photoId'] = $avatorUrls[$v['photo']]['id']??'';
            $list[$k]['title'] = htmlspecialchars_decode($v['title'],ENT_QUOTES);
        }
        return Basic::data(0, 'common.success', ['list' => $list, 'total' => $total]);
    }


    /**
     * @describe 登录验证
     * @author: 徐众
     * @date: 2025-04-02 13:02:26
     * @param [] type
     * @return Renderable
     */
    public function detail($params): array
    {
        #参数校验
        if(!isset($params['id']) || empty($params['id'])){
            return Basic::data( 100002, 'common.parameterError');
        }
        $data = $this->model->find($params['id']);
        if(!empty($data)){
            $data = $data->toArray();
        }else{
            $data = [];
        }
        if(isset($data['content']) && $data['content']){
            $data['content'] = htmlspecialchars_decode($data['content'],ENT_QUOTES);
        }
        if(isset($data['title']) && $data['title']){
            $data['title'] = htmlspecialchars_decode($data['title'],ENT_QUOTES);
        }
        return Basic::data(0, 'common.success',$data);
    }

}